import { BasicColumn, BasicTable, FormSchema, useRender, useTable } from "@/components/Table";
import { message, Image } from "antd";
import { productApi } from "@/api/erp/drug/drug";
import { useTranslation } from "react-i18next";
import Toolbar from "@/components/ViewComponents/Toolbar";
import CreateAndUpdata from "./components/createAndUpdata";
import { useDrawer } from "@/components/Drawer";
import { exportToExcel, mergeObjects } from "@/utils/util";
import { IconEnum } from "@/enums/appEnum";
import TableAction from "@/components/Table/components/TableAction";
import Part from "./components/part";
const index = () => {
	const { t } = useTranslation();
	const en_vi = "drugStorage.equipment.device";
	const schemas: FormSchema[] = [
		{
			label: t(`${en_vi}.eqpId`),
			field: "eqpId",
			component: "Input"
		},
		{
			label: t(`${en_vi}.eqpName`),
			field: "eqpName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.orgId`),
			field: "orgId",
			component: "Input"
		}
		// {
		// 	label: t("felix_ui.status"),
		// 	component: "Select",
		// 	field: "status",
		// 	componentProps: {
		// 		options: [
		// 			{ label: t(`drugStorage.notStarted`), value: "-1" },
		// 			{ label: t(`drugStorage.inApproval`), value: "1" },
		// 			{ label: t(`drugStorage.approved`), value: "2" },
		// 			{ label: t(`drugStorage.approvalNotApproved`), value: "3" },
		// 			{ label: t(`drugStorage.cancelled`), value: "4" }
		// 		]
		// 	}
		// }
	];
	const columns: BasicColumn[] = [
		{
			title: t(`${en_vi}.eqpId`),
			dataIndex: "eqpId"
		},
		{
			title: t(`${en_vi}.eqpName`),
			dataIndex: "eqpName"
		},
		{
			title: t(`${en_vi}.eqpImage`),
			dataIndex: "eqpImage",
			render: (eqpImage: string) => {
				return <Image width={50} src={eqpImage} />;
			}
		},
		{
			title: t(`${en_vi}.genVno`),
			dataIndex: "genVno"
		},
		{
			title: t(`${en_vi}.orgId`),
			dataIndex: "orgId"
		},
		{
			title: t(`${en_vi}.proDtime`),
			dataIndex: "proDtime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.buyDtime`),
			dataIndex: "buyDtime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.expDtime`),
			dataIndex: "expDtime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.qltyGuarFrom`),
			dataIndex: "qltyGuarFrom",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.qltyGuarTo`),
			dataIndex: "qltyGuarTo",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.catOuter`),
			dataIndex: "catOuter"
		},
		{
			title: t(`${en_vi}.catInner`),
			dataIndex: "catInner"
		},
		{
			title: t(`${en_vi}.subClass`),
			dataIndex: "subClass"
		},
		{
			title: t(`${en_vi}.shelfSq`),
			dataIndex: "shelfSq"
		},
		{
			title: t(`${en_vi}.unitPrice`),
			dataIndex: "unitPrice"
		},
		{
			title: t(`${en_vi}.unitName`),
			dataIndex: "unitName"
		},
		{
			title: t(`${en_vi}.smktPrice`),
			dataIndex: "smktPrice"
		},
		{
			title: t(`${en_vi}.umktPrice`),
			dataIndex: "umktPrice"
		},
		{
			title: t(`${en_vi}.invoiceTag`),
			dataIndex: "invoiceTag"
		},
		{
			title: t(`${en_vi}.einvoiceTag`),
			dataIndex: "einvoiceTag"
		},
		{
			title: t(`${en_vi}.invoiceTaxRate`),
			dataIndex: "invoiceTaxRate"
		},
		{
			title: t(`${en_vi}.invoiceTinSno`),
			dataIndex: "invoiceTinSno"
		},
		// {
		// 	title: t("felix_ui.status"),
		// 	dataIndex: "status",
		// 	render: (_, { status }) => {
		// 		switch (status) {
		// 			case "-1":
		// 				return <Tag color="default">{t(`drugStorage.notStarted`)}</Tag>;
		// 			case "1":
		// 				return <Tag color="processing">{t(`drugStorage.inApproval`)}</Tag>;
		// 			case "2":
		// 				return <Tag color="success">{t(`drugStorage.approved`)}</Tag>;
		// 			case "3":
		// 				return <Tag color="warning">{t(`drugStorage.approvalNotApproved`)}</Tag>;
		// 			case "4":
		// 				return <Tag color="error">{t(`drugStorage.cancelled`)}</Tag>;
		// 			default:
		// 				break;
		// 		}
		// 	}
		// },
		{
			title: t("felix_ui.action"),
			dataIndex: "action",
			fixed: "right",
			render: (_: any, record: any) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.edit"),
							onClick: handleEdit.bind(null, record)
						}
					]}
					dropDownActions={[
						{
							icon: IconEnum.EDIT,
							label: t(`${en_vi}.part`),
							onClick: handleAddPart.bind(null, record)
						},
						{
							icon: IconEnum.DELETE,
							danger: true,
							label: t("felix_ui.delete"),
							popConfirm: {
								title: t("felix_ui.delMessage"),
								placement: "top",
								confirm: handleDelete.bind(null, record.productId)
							}
						}
					]}
				></TableAction>
			)
		}
	];
	const [registerTable, { reload }] = useTable({
		api: productApi.getproductPage,
		columns,
		formConfig: {
			labelWidth: 150,
			labelWrap: true,
			schemas,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 }
		},
		bordered: true,
		useSearchForm: true,
		searchInfo: {
			bizType: "3"
		},
		afterFetch: (res: any) => {
			return res.map((item: any) => mergeObjects(item, item.equipments));
		}
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	const [registerAddPart, { openDrawer: openDrawerAddPart }] = useDrawer();
	const handleEdit = (record: any) => {
		openDrawer(true, {
			reload,
			record,
			isUpdated: true
		});
	};
	//表格导出
	const exportExcel = async () => {
		await productApi.getproductPage({}).then(res => {
			exportToExcel(res.list, t("felix_ui.pat_basic_info"));
		});
	};
	//删除角色
	const handleDelete = async (id: any) => {
		// 发起删除
		const res = await productApi.deleteproduct(id, "3");
		reload();
		if (!res) return message.error(t("viewComponents.patientInformation.deleteFailed"));
		message.success(t("viewComponents.patientInformation.deleteSuccessfully"));
	};
	//添加零件
	const handleAddPart = (record: any) => {
		console.log(record);
		openDrawerAddPart(true, {
			record
		});
	};
	const handlerToolbarAction = async (key: string) => {
		switch (key) {
			case "add":
				openDrawer(true, {
					reload,
					isUpdated: false
				});
				break;
			case "export":
				exportExcel();
				break;
		}
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
				<CreateAndUpdata register={registerDrawer}></CreateAndUpdata>
				<Part register={registerAddPart}></Part>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={["add", "export"]} />
		</div>
	);
};
export default index;
